using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Fieldbook.Entities;
using System.Collections.Generic;
using Fieldbook.Business;

namespace Fieldbook.Testwebsite.User
{
    public partial class StorageOverview : FieldbookPage
    {
        private List<Resource> resources = null;

        protected override void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
            resources = ResourceController.LoadResources((long)Session["userID"]);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            long userID = (long)Session["userID"];
            long categoryID = Int64.Parse(dgvCategories.SelectedDataKey.Value.ToString());
            List<StorageItem> items = StorageController.LoadStorageItems(userID, categoryID);
            dgvStorage.DataSource = items;
            dgvStorage.DataBind();
        }

        protected void dgvStorage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                Label lblResource = (Label)e.Row.Cells[0].FindControl("ResourceID");
                long resourceID = Int64.Parse(lblResource.Text);
                Resource resource = resources.Find(delegate(Resource r) { return (long)r.ResourceID == resourceID; });
                lblResource.Text = resource.Description;
            }
        }

        /// <summary>
        /// Adding new resource category
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkAddCategory_Command(object sender, CommandEventArgs e)
        {
            bool sucess = false;
            if (e.CommandArgument.ToString() == "global")
            {
                sucess = ResourceController.InsertResourceCategory(txtResourceCategory.Text);
            }
            else
            {
                long userID = (long)Session["userID"];
                sucess = ResourceController.InsertResourceCategory(txtResourceCategory.Text, userID);
            }
            if (sucess)
            {
                this.lblOperationStatus.Text = "Category sucessfully added";
                this.txtResourceCategory.Text = "";
                this.dgvCategories.DataBind();
                this.ddlResourceCategory.DataBind();
            }
            else
            {
                this.lblOperationStatus.Text = "Error adding category";
            }
        }
        
        /// <summary>
        /// Adding new resource type
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkAddResource_Command(object sender, CommandEventArgs e)
        {
            bool sucess = false;
            long userID = (long)Session["userID"];
            long resourceCategory = Int64.Parse(ddlResourceCategory.SelectedValue);
            int mesaureUnit = Int32.Parse(ddlMeasureUnit.SelectedValue);
            string description = txtResource.Text;

            if (e.CommandArgument.ToString() == "global")
            {
                sucess = ResourceController.InsertResource(resourceCategory, mesaureUnit, description);
            }
            else
            {
                sucess = ResourceController.InsertResource(resourceCategory, mesaureUnit, description, userID);
            }
            if (sucess)
            {
                lblOperationStatus.Text = "Resource sucessfully added";
                txtResource.Text = "";
            }
            else
            {
                lblOperationStatus.Text = "Error adding resource";
            }
        }
    }
}
